package com.shujia.java.day3;

public class Demo24etos {
    public static void main(String[] args) {
        /**
         * 将十进制的整数转换成二进制字符串
         *
         */

        int a = 12;

        String b = "";//保存二进制结果

        while (a != 0) {

            int i = a % 2;
            b = i + b;//将余数拼接到二进制中
            a = a / 2;//计算商

        }

        System.out.println(a + " 的二进制是 " + b);

        /**
         *
         * 将十进制小数转换成二进制
         * 方法：乘二取整
         * 0.25
         * 0.25 * 2 = 0.5
         * 0.5 * 2 = 1.0
         *
         *
         * 0 1 0
         */
        String b2 = "";//保存二进制结果

        double d = 0.25;

        int i = 0;

        while (d != 0.0) {
            double v = d * 2;
            int v1 = (int) v;

            b2 = b2 + v1;

            d = v - v1;

            i++;

            if (i == 10) {
                break;
            }
        }

        System.out.println(b2);


    }
}
